package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

public class InfoCAView {
    private final static String CA_KEYS[] = {
    		DeviceInfoKey.K.CA_NUMBER,
            DeviceInfoKey.K.CA_FACING,
    		DeviceInfoKey.K.CA_PIXELS,
    		DeviceInfoKey.K.CA_PIC_FMT,
    		DeviceInfoKey.K.CA_PIC_SIZE,
    		DeviceInfoKey.K.CA_PV_FMT,
    		DeviceInfoKey.K.CA_PV_SIZE,
    		DeviceInfoKey.K.CA_PV_FRATE,
    		DeviceInfoKey.K.CA_PV_FPS,
    		DeviceInfoKey.K.CA_VSIZE,
    		DeviceInfoKey.K.CA_ABAND,
    		DeviceInfoKey.K.CA_CEFFECT,
    		DeviceInfoKey.K.CA_FLASH,
    		DeviceInfoKey.K.CA_FOCUS,
    		DeviceInfoKey.K.CA_THUMB_SIZE,
    		DeviceInfoKey.K.CA_SCENE,
    		DeviceInfoKey.K.CA_WBAL,
    		DeviceInfoKey.K.CA_ZOOM,
    		DeviceInfoKey.K.CA_SMOOTH_ZOOM,
    		DeviceInfoKey.K.CA_ORIENTATION
    };
    
    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoCAView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
	public View
	getView(boolean for_preview) {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);
        
        int childno = 0;
        for (int i=0; i < InfoCAView.CA_KEYS.length; i++) {
            String info = _di.GetString(CA_KEYS[i]);
            if (TextUtils.isEmpty(info) || info.equals(CONSTS.UNAVAILABLE))
                continue;
            
        	if (TextUtils.equals(CA_KEYS[i], DeviceInfoKey.K.CA_PIXELS)) {
        		JSONArray features = _di.GetJSONArray(CA_KEYS[i]);
        		if (features != null && features.length() > 0) {
        			info = "";
        			for (int j=0; j<features.length(); j++) {
            			long pixels = 0;
						try {
							pixels = Long.parseLong(features.getString(j));
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
						} catch (JSONException e) {
							// TODO Auto-generated catch block
						}
						if (pixels > 0) {
							if (j >0)
								info += "\n";
                			long m_v = pixels / InfoViewCONSTS.M_LV; 
                			if (m_v > 0) {
                				double mi_v = ((double)pixels)/((double)InfoViewCONSTS.M_LV);
                				info += new DecimalFormat("#.##").format(mi_v) + 
                						_cxt.getResources().getString(R.string.di_ca_m_pixel);
                			}
                			else {
                				double mi_v = ((double)pixels)/((double)InfoViewCONSTS.K_LV);
                				info += new DecimalFormat("#.##").format(mi_v) + 
                						_cxt.getResources().getString(R.string.di_ca_k_pixel);
                			}            				
						}
        			}
        		}
        		else {
        			long pixels = Long.parseLong(_di.GetString(CA_KEYS[i]));
        			long m_v = pixels / InfoViewCONSTS.M_LV; 
        			if (m_v > 0) {
        				double mi_v = ((double)pixels)/((double)InfoViewCONSTS.M_LV);
        				info = new DecimalFormat("#.##").format(mi_v) + " " + 
        						_cxt.getResources().getString(R.string.di_ca_m_pixel);
        			}
        			else {
        				double mi_v = ((double)pixels)/((double)InfoViewCONSTS.K_LV);
        				info = new DecimalFormat("#.##").format(mi_v) + " " + 
        						_cxt.getResources().getString(R.string.di_ca_k_pixel);
        			}
        		}
        	}
            else if (TextUtils.equals(CA_KEYS[i], DeviceInfoKey.K.CA_FACING)) {
                JSONArray features = _di.GetJSONArray(CA_KEYS[i]);
                info = "";
                switch (features.length()) {
                case 0: // none info...
                    break;
                case 1: // only one item.
                    info = getFacingType(features, 0);
                    break;
                default:    // more than one items.
                    for (int j=0; j<features.length(); j++) {
                        if (j>0)
                            info += "\n";
                        info += getFacingType(features, j); 
                    }
                    break;
                }
            }
        	else if (!TextUtils.equals(CA_KEYS[i], DeviceInfoKey.K.CA_NUMBER)) {
            	ArrayList<String> sub_info = getSubInfo(CA_KEYS[i]);
            	info = "";
            	switch (sub_info.size()) {
            	case 0:	// none info...
            		break;
            	case 1:	// only one item.
            		info = sub_info.get(0);
            		break;
            	default:	// more than one items.
            		for (int j=0; j<sub_info.size(); j++) {
            			if (j>0)
            				info += "\n";
            			info += "[ " + sub_info.get(j) + " ]"; 
            		}
            		break;
            	}
            }
            
            if (!TextUtils.isEmpty(info)) {
            	View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item, null);
            	InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
            	row.setTag(wrapper);
            	wrapper.setItemName(DeviceInfoKey.GetName(CA_KEYS[i]));
            	wrapper.setItemDetail(info);
            	sv.addView(row);
            	childno++;
            }
        }
        return sv;
	}
	
	private String
	getFacingType(JSONArray nTypes, int nIdx)
	{
	    int nType = -1;
	    try {
	        nType = nTypes.getInt(nIdx);
	    } catch (JSONException e1) {
	        try {
	            return nTypes.getString(nIdx);
	        } catch (JSONException e2) {
	        }
	    }
	    
	    //CameraInfo is defined in SDK 9 so we have to use constant here
	    //CameraInfo.CAMERA_FACING_BACK: 0
	    //CameraInfo.CAMERA_FACING_BACK: 1
	    if (nType == 0)
	        return _cxt.getString(R.string.di_ca_back);
	    else if (nType == 1)
            return _cxt.getString(R.string.di_ca_front);
	    return CONSTS.UNKNOWN;
	}
	
	private ArrayList<String>
	getSubInfo(String key)
	{
	    boolean bFPS = key.equals(DeviceInfoKey.K.CA_PV_FPS);
	    
		ArrayList<String> sub_infos = new ArrayList<String>();
    	JSONArray features = _di.GetJSONArray(key);
		if (features != null && features.length() > 0) {
			for (int j=0; j<features.length(); j++) {
		    	String sub_info = "";
				JSONArray sub_features = null;
				try {
					sub_features = features.getJSONArray(j);
				}
				catch (JSONException e) {
				}
					
				if (sub_features == null) {
					try {
						if (!features.getString(j).equals(CONSTS.UNAVAILABLE)) {
  							//if (j != 0)
  							//	sub_info += ", ";
							sub_info += features.getString(j);
						}
					} 
					catch (JSONException e) {
					}
				}
				else {
					JSONArray s3_f = null;
					for (int m=0; m<sub_features.length(); m++) {
						if (m != 0)
							sub_info += ", ";
						try {
							s3_f = sub_features.getJSONArray(m);
						}
						catch (JSONException e) {
							//e.printStackTrace();
						}
						if (s3_f == null) {
							try {
								if (!sub_features.getString(m).equals(CONSTS.UNAVAILABLE)) {
									sub_info += sub_features.getString(m);
								}
							} 
							catch (JSONException e) {
							}
						}
						else {
						    if (bFPS) {
						        for (int k=0; k<s3_f.length(); k++) {
                                    try {
                                        if (k != 0)
                                            sub_info += "-";
                                        float f = s3_f.getInt(k) / 1000.0f;
                                        sub_info += f;
                                    } 
                                    catch (JSONException e) {
                                    }
                                }
						    }
						    else {
    							for (int k=0; k<s3_f.length(); k++) {
    								try {
    									if (k != 0)
    										sub_info += "x";
    									sub_info += s3_f.getString(k);
    								} 
    								catch (JSONException e) {
    								}
    							}
						    }
						}
					}	// end for
				}	// end if-else
				if (!TextUtils.isEmpty(sub_info) && !sub_info.equals(CONSTS.UNAVAILABLE))
					sub_infos.add(sub_info);
			}	// end for
		}	// end if
		return sub_infos;
	}
}
